package com.offcn.project.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.project.po.*;
import com.offcn.project.service.ProjectInfoService;
import com.offcn.project.vo.resp.ProjectDetailVo;
import com.offcn.project.vo.resp.ProjectVo;
import com.offcn.utils.OssTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags = "项目处理接口")
@RestController
@RequestMapping("project")
public class ProjectInfoController {

    //注入OssTemplate
    @Autowired
    private OssTemplate ossTemplate;

    //注入项目信息服务接口
    @Autowired
    private ProjectInfoService projectInfoService;

    @ApiOperation("上传文档接口")
    @PostMapping("upload")
    public AppResponse<Map<String,Object>> upload(@RequestParam("file") MultipartFile[] files) throws IOException {
        Map<String,Object> map=new HashMap<>();
        //创建一个list集合，存储全部上传文件名称
        List<String> list=new ArrayList<>();
        //判断上传文件数组如果不为空
        if(files!=null&&files.length>0){
            //遍历上传文件数组
            for (MultipartFile file : files) {
                //判断上传文件不为空
                if(!file.isEmpty()){

                    //调用ossTemplate执行上传
                    String uploadFile = ossTemplate.upload(file.getInputStream(), file.getOriginalFilename());
                    list.add(uploadFile);
                }
            }
        }

        //把上传成功的文件集合封装到map
        map.put("urls",list);

        return AppResponse.ok(map);
    }



    @ApiOperation("根据项目编号，查询对应回报信息")
    @GetMapping("/details/returns/{projectId}")
    public AppResponse<List<TReturn>> detailsReturn(@PathVariable("projectId") Integer projectId){
        List<TReturn> returnList = projectInfoService.getProjectReturns(projectId);

        return AppResponse.ok(returnList);

    }


    @ApiOperation("获取系统全部项目")
    @GetMapping("all")
    public AppResponse<List<ProjectVo>> all(){
        List<ProjectVo> projectVoList=new ArrayList<>();
        //调用service查询全部项目
        List<TProject> projectList = projectInfoService.getAllProjects();
        //遍历全部的项目集合
        for (TProject project : projectList) {
            //创建一个项目响应的vo对象
            ProjectVo projectVo = new ProjectVo();
            //获取项目编号
            Integer projectId = project.getId();
            //跟据项目编号获取对应配图
            List<TProjectImages> projectImagesList = projectInfoService.getProjectImages(projectId);
            //遍历全部的项目配图
            for (TProjectImages projectImages : projectImagesList) {
                //判断项目配图的类型，如果是头图，把头图值设置到projectVo头图属性
                if(projectImages.getImgtype()==0){
                    projectVo.setHeadImage(projectImages.getImgurl());
                }
            }
            //使用bean工具类，复制项目对象属性值到projectVo
            BeanUtils.copyProperties(project,projectVo);
            //把vo保存到list集合
            projectVoList.add(projectVo);


        }

        return AppResponse.ok(projectVoList);
    }

    //
    @ApiOperation("获取项目详情")
    @GetMapping("/details/info/{projectId}")
    public AppResponse<ProjectDetailVo> detailsInfo(@PathVariable("projectId") Integer projectId){
        TProject tProject = projectInfoService.getProjectInfo(projectId);
        //创建一个项目VO对象
        ProjectDetailVo projectDetailVo = new ProjectDetailVo();

        //1、读取项目配图
        List<TProjectImages> projectImages = projectInfoService.getProjectImages(projectId);
        //创建一个单独的集合存储项目详情图
        List<String> detailsImages=new ArrayList<>();
        //遍历项目配图的集合
        for (TProjectImages projectImage : projectImages) {
            //判断项目配图类型，如果是头图，设置项目详情Vo的头图属性
            if (projectImage.getImgtype()==0){
                projectDetailVo.setHeaderImage(projectImage.getImgurl());
            }else {
                //项目详情图，统一添加到项目详情图的集合
                detailsImages.add(projectImage.getImgurl());
            }
        }

        //把项目详情图的集合关联设置到项目详情Vo详情图属性
        projectDetailVo.setDetailsImage(detailsImages);

        //2、读取项目回报
        List<TReturn> projectReturns = projectInfoService.getProjectReturns(projectId);
        //设置项目回报到项目详情Vo
        projectDetailVo.setProjectReturns(projectReturns);

        //3、使用bean工具类，复制项目其他属性值
        BeanUtils.copyProperties(tProject,projectDetailVo);

        //返回项目详细信息对象
        return AppResponse.ok(projectDetailVo);
    }

    //
    @ApiOperation("获取全部的项目标签")
    @GetMapping("tags")
    public AppResponse<List<TTag>> tags(){
        List<TTag> tagList = projectInfoService.getAllprojectTags();
        return AppResponse.ok(tagList);
    }

    @ApiOperation("获取全部的项目分类")
    @GetMapping("types")
    private AppResponse<List<TType>> types(){
        List<TType> typeList = projectInfoService.getAllProjectTypes();
        return AppResponse.ok(typeList);
    }

    @ApiOperation("获取指定项目回报的详细信息")
    @GetMapping("/returns/info/{returnId}")
    public AppResponse<TReturn> getReturnInfo(@PathVariable("returnId") Integer returnId){

        TReturn tReturn = projectInfoService.getReturnInfo(returnId);

        return AppResponse.ok(tReturn);
    }
}
